package com.qky.controller.utilController;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UpLoadImgController {
	/**
	 * 上传头像
	 * @return "user/show1"
	 */
	@RequestMapping(value = "/uploadHeadImg",method=RequestMethod.POST)
	@ResponseBody
	public ModelMap uploadHeadImgUserPost(MultipartFile file,
			HttpServletRequest request, ModelMap model) { 
	    System.out.println("开始"); 
	    String imgPath="resources/upload/";
	    String path = request.getSession().getServletContext().getRealPath(imgPath);  
	    String fileName = file.getOriginalFilename();  
	    //String fileName = new Date().getTime()+".jpg";  
	    System.out.println(path);  
	    File targetFile = new File(path, fileName);  
	    if(!targetFile.exists()){  
	        targetFile.mkdirs();  
	    }  
	    //保存  
	    try {  
	        file.transferTo(targetFile);  
	    } catch (Exception e) {  
	        e.printStackTrace();  
	    }//request.getContextPath()
	    model.addAttribute("fileUrl",imgPath+fileName);
	    System.out.println(model.get("fileUrl"));
	    return model;  
	}  
}
